using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using MCMLXVII.Tools.OrganizationChart.MembersClasses;
using MCMLXVII.BackEnd.Services.OrganizationChart.Interfaces;
using MCMLXVII.BackEnd.Services.OrganizationChart.Messages;
using MCMLXVII.Core.Common;

namespace MCMLXVII.Tools.OrganizationChart.SmartEditors
{
    public partial class ucSmartRoleEditor : ucSmartBaseEditor 
    {
        public override event EventHandler<MemberUpdatedEventsArgs> EditorClosed;
        public override event EventHandler<MemberUpdatedEventsArgs> MemberUpdated;
        private bool Loading = false;

        private MemberRole _Member;
        private int _NodeID;
        public override int NodeID
        {
            get
            {
                return _NodeID;
            }
        }

        public ucSmartRoleEditor(MemberRole Member, int NodeID):base()
        {
            _NodeID = NodeID;
            InitializeComponent();
            _Member = Member;
            base.CancelRequested += new EventHandler(ucSmartEditor_CancelRequested);
            base.SaveRequested += new EventHandler(ucSmartEditor_SaveRequested);
            base.pboxItemClass.Image = (System.Drawing.Image)Properties.Resources.icoCargo.ToBitmap();
        }

        void ucSmartEditor_SaveRequested(object sender, EventArgs e)
        {
            if (MemberUpdated != null)
            {
                iOrganizationTreeMemberClass EditingObject = (iOrganizationTreeMemberClass)_Member;
                msgOrganizationMemberInfo info = EditingObject.GetMemberData();
                MemberUpdated.Invoke(this, new MemberUpdatedEventsArgs(_Member.MemberID, _NodeID, EditingObject));
                base.DisableUpdate();
            }
        }

        void ucSmartEditor_CancelRequested(object sender, EventArgs e)
        {
            if (EditorClosed != null)
            {
                EditorClosed.Invoke(this, new MemberUpdatedEventsArgs(_Member.MemberID));
            }
        }


        private void ucSmartNodeEditor_Load(object sender, EventArgs e)
        {

            Loading = true;
            txtNodeID.Text = _Member.MemberID.ToString();
            txtNodeName.Text = _Member.Description;

            txtAsignedToUserID.Text = _Member.RoleMemberID.MemberID.ToString();
            txtAsignedToUserDescription.Text = _Member.RoleMemberID.Description;
            txtAssignedToUserInfo.Text = "...";

            foreach (msgMembershipInfo Member in _Member.MemberOf)
            {
                grdTranslations.Rows.Add(Member.MemberID, Member.Description);
            }
            base.RelocateButtons(btnChangeGroup.Top + btnChangeGroup.Height);
            Loading = false;

        }

        private void txtNodeName_TextChanged(object sender, EventArgs e)
        {
            if (!Loading)
            {
                _Member.Description = txtNodeName.Text;
                base.EnableUpdate();
            }
        }

        private void btnChangeGroup_Click(object sender, EventArgs e)
        {
            frmMembersEditor frmDialog = new frmMembersEditor(_Member.MemberOf, _Member.MemberClass, _Member.Description);
            System.Windows.Forms.DialogResult res = frmDialog.ShowDialog(Program.frm);
            if (res == System.Windows.Forms.DialogResult.OK)
            {
                _Member.MemberOf = frmDialog.ActualMembers;
                grdTranslations.Rows.Clear();
                foreach (msgMembershipInfo Member in _Member.MemberOf)
                {
                    grdTranslations.Rows.Add(Member.MemberID, Member.Description);
                }
                base.EnableUpdate();
            }

        }



        private void btnSelUser_Click(object sender, EventArgs e)
        {
            frmMembersSelector frmDialog = new frmMembersSelector();
            System.Windows.Forms.DialogResult res = frmDialog.ShowDialog();
            if (res != System.Windows.Forms.DialogResult.Cancel)
            {
                txtAsignedToUserID.Text = frmDialog.ActualMember.MemberID.ToString();
                txtAsignedToUserDescription.Text = frmDialog.ActualMember.Description;
                txtAssignedToUserInfo.Text = "...";
                _Member.RoleMemberID = frmDialog.ActualMember;
                base.EnableUpdate();
            }
        }

        private void label6_Click(object sender, EventArgs e)
        {

        }

        private void groupBox1_Enter(object sender, EventArgs e)
        {

        }

    }
}
